package edu.whu.demo.dao;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import edu.whu.demo.domain.Product;
import edu.whu.demo.domain.ProductDTO;
import org.apache.ibatis.annotations.*;

// 定义一个名为ProductDao的接口，继承自BaseMapper<Product>
@Mapper
public interface ProductDao extends BaseMapper<Product> {

    // 插入产品供应商信息的方法a
    @Insert("insert into product_supplier (product_id,supplier_id) values(#{productId},#{supplierId})")
    public void insertProductSupplier(long productId, long supplierId);

    // 删除指定产品的所有供应商信息的方法
    @Delete("delete from product_supplier where product_id = #{productId}")
    public void deleteSuppliersOfProduct(long productId);

    // 根据供应商查询产品的分页方法
    @Select("select distinct p.* from product p" +
            " left join product_supplier ps on p.id=ps.product_id " +
            " left join supplier s on s.id=ps.supplier_id " +
            " ${ew.customSqlSegment}") //由QueryWrapper转换
    @Results({
            @Result(id = true, property = "id", column = "id"),
            @Result(property = "supplierList", column = "id",
                    many = @Many(select = "edu.whu.demo.dao.SupplierDao.findSuppliersByProduct"))
    })
    public IPage<ProductDTO> findProductsBySupplier(IPage<ProductDTO> page,
                                                    @Param(Constants.WRAPPER) QueryWrapper<ProductDTO> wrapper);

    // 根据条件查询产品的分页方法
    @Select("select p.* from product p ${ew.customSqlSegment}")
    @Results({
            @Result(id = true, property = "id", column = "id"),
            @Result(property = "supplierList", column = "id",
                    many = @Many(select = "edu.whu.demo.dao.SupplierDao.findSuppliersByProduct"))
    })
    public IPage<ProductDTO> findProducts(IPage<ProductDTO> page,
                                          @Param(Constants.WRAPPER) QueryWrapper<ProductDTO> wrapper);
}

